package com.eightbears.beboy.ui.user.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.eightbears.beboy.R;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.CommonUtils;
import com.eightbears.beboy.base.util.ErrorUtils;
import com.eightbears.beboy.base.util.GsonUtil;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.ShowToast;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.entity.OrderEntity;
import com.eightbears.beboy.entity.status.Status_Error;
import com.socks.library.KLog;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.STATUS_ALREADY_COMMENT;
import static com.eightbears.beboy.base.util.CommonAPI.URL_SHOPS_COMMENT;
import static com.eightbears.beboy.ui.user.fragment.WaitUseOrderFragment.PARAMS_ORDER_LIST;

public class ShopCommentActivity extends BaseActivity {

    @BindView(R.id.sw_finish)
    SwipeBackLayout sw_finish;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.iv_shop_image)
    ImageView iv_shop_image;
    @BindView(R.id.tv_shop_name)
    TextView tv_shop_name;
    @BindView(R.id.rb_comment_start)
    RatingBar rb_comment_start;
    @BindView(R.id.et_comment_share)
    EditText et_comment_share;
    @BindView(R.id.btn_comment_send)
    TextView btn_comment_send;

    private OrderEntity.OrderInfo orderInfo;
    private String starNum, comment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_comment);

        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        tv_title.setText(getString(R.string.text_comment));
    }

    @Override
    protected void initData() {
        orderInfo = (OrderEntity.OrderInfo) getIntent().getExtras().get(PARAMS_ORDER_LIST);
        if (orderInfo != null) {
            KLog.e(orderInfo);
            CommonUtils.loadImageView(this, orderInfo.getShop().getCover(),
                    R.mipmap.icon_default_shop, iv_shop_image);
            tv_shop_name.setText(orderInfo.getShop().getTitle());
        }
    }

    @Override
    protected void initEvent() {
        sw_finish.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                finish();
            }
        });
    }

    @OnClick(R.id.ll_back)
    void back() {
        finish();
    }

    @OnClick(R.id.btn_comment_send)
    void commentSend() {
        if (checkInput()) {
            OkHttpUtils.post().url(URL_SHOPS_COMMENT)
                    .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                    .addParams("id", orderInfo.getCoupon().getShop_id())
                    .addParams("score", starNum)//星星
                    .addParams("comment", comment)//评论内容
                    .addParams("purchase_id", orderInfo.getId())
                    .build().execute(new StringCallback() {

                @Override
                public void onResponse(String response) {
                    ShowToast.showShortToast(R.string.alert_comment_success);
                    EventBus.getDefault().post(STATUS_ALREADY_COMMENT);
                    finish();
                }

                @Override
                public void onError(Call call, Exception e) {
                    Status_Error status_error = GsonUtil.fromJson(e.getMessage(), Status_Error.class);
                    ErrorUtils.alertError(ShopCommentActivity.this, status_error);
                }
            });
        }
    }

    private boolean checkInput() {
        starNum = String.valueOf(((int)(rb_comment_start.getRating())));
        comment = et_comment_share.getText().toString();
        if (starNum.equals("0")) {
            ShowToast.showShortToast(getString(R.string.alert_grade));
            return false;
        }

        if (TextUtils.isEmpty(comment)) {
            ShowToast.showShortToast(R.string.alert_input_empty);
            return false;
        }

        return true;
    }
}
